﻿using RFIDSystem.IServices;
using RFIDSystem.Model.Models;
using RFIDSystem.Model.ViewModels;
using RFIDSystem.Services.BASE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RFIDSystem.Services
{
    public class RfidServices : BaseServices<RFID>, IRfidServices
    {
        public async Task<bool> AddRFID(RFID input)
        {
            var bloglist = await base.Add(input);

            return true;
        }

        public async Task<bool> DelRFID(string id)
        {
            var bloglist = await base.DeleteById(id);

            return true;
        }

        public async Task<List<RFIDViewModel>> GetRFIDs()
        {
            List<RFIDViewModel> rvs = new List<RFIDViewModel>();
            var rs = await base.Query(q => q.ParentId == 0);
            var rcs = await base.Query(q => q.ParentId != 0);
            foreach (var r in rs)
            {
                RFIDViewModel rv = new RFIDViewModel();
                rv.Id = r.Id;
                rv.Name = r.Name;
                rv.position[0] = r.Lat;
                rv.position[1] = r.lng;
                rv.ParentId = r.ParentId;
                var rcv = rcs.Where(qr => qr.ParentId == r.Id).ToList();
                if (rcv.Any())
                {
                    List<RFIDViewModel> rcvs = new List<RFIDViewModel>();
                    foreach (var rc in rcv)
                    {
                        RFIDViewModel rr = new RFIDViewModel();
                        rr.Id = rc.Id;
                        rr.Name = rc.Name;
                        rr.position[0] = rc.Lat;
                        rr.position[1] = rc.lng;
                        rr.ParentId = rc.ParentId;
                        rcvs.Add(rr);
                    }
                    rv.children .AddRange( rcvs);
                }
                rvs.Add(rv);
            }
            return rvs;
        }
    }
}